package com.ddtech.business.task.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.course.entity.CourseStudyRecord;
import com.ddtech.business.course.query.CourseStudyRecordQuery;
import com.ddtech.business.course.service.CourseStudyRecordService;
import com.ddtech.business.task.entity.TaskUser;
import com.ddtech.business.task.mapper.TaskUserMapper;
import com.ddtech.business.task.service.TaskUserService;
import com.ddtech.business.task.vo.req.TaskReqVo;
import com.ddtech.business.task.vo.req.TaskUserReqVo;
import com.ddtech.framework.mapper.UserVoMapper;
import com.ddtech.framework.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 任务学员关联表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Service
public class TaskUserServiceImpl extends ServiceImpl<TaskUserMapper, TaskUser> implements TaskUserService {
    @Resource
    private UserVoMapper userVoMapper;
    @Autowired
    private CourseStudyRecordService courseStudyRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTaskUser(TaskReqVo taskReqVo) {
        if (CollUtil.isNotEmpty(taskReqVo.getTaskUser())) {
            String taskId = taskReqVo.getId();
            QueryWrapper<TaskUser> queryWrapper = new QueryWrapper();
            queryWrapper.eq("TASK_ID", taskId);
            //所有数据
            List<TaskUser> taskUsers = this.list(queryWrapper);
            //新加数据
            List<TaskUserReqVo> taskUserReqVos = taskReqVo.getTaskUser();
            Set<TaskUser> removeIds = new HashSet<>();
            for (TaskUser taskUser : taskUsers) {
                boolean flag = false;
                for (TaskUserReqVo taskUserReqVo : taskUserReqVos) {
                    if (taskUserReqVo.getUserId().equals(taskUser.getUserId())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    removeIds.add(taskUser);
                }
            }
            if (CollUtil.isNotEmpty(removeIds)) {
                removeIds.forEach(item -> {
                    CourseStudyRecordQuery query = new CourseStudyRecordQuery();
                    query.setTaskId(item.getTaskId());
                    query.setUserId(item.getUserId());
                    List<CourseStudyRecord> courseStudyRecordList = courseStudyRecordService.list(query.autoWrapper());
                    if (CollUtil.isNotEmpty(courseStudyRecordList)) {
                        courseStudyRecordList.forEach(courseStudyRecord -> {
                            courseStudyRecord.baseDelete();
                            courseStudyRecordService.saveOrUpdate(courseStudyRecord);
                        });
                    }
                    this.removeById(item.getId());
                });
            }
            taskUserReqVos.forEach(item -> {
                QueryWrapper<TaskUser> taskQuery = new QueryWrapper();
                taskQuery.eq("TASK_ID", taskId);
                taskQuery.eq("USER_ID", item.getUserId());
                TaskUser taskUser = this.getOne(taskQuery);
                if (taskUser == null) {
                    taskUser = new TaskUser();
                }
                BeanUtil.copyProperties(item, taskUser, CopyOptions.create().setIgnoreCase(true));
                taskUser.setTaskId(taskId);
                this.saveOrUpdate(taskUser);
            });
        }
    }

    @Override
    public List<TaskUserReqVo> getUser(String id) {
        QueryWrapper<TaskUser> removeWrapper = new QueryWrapper();
        removeWrapper.eq("TASK_ID", id);
        List<TaskUser> list = this.list(removeWrapper);
        List<TaskUserReqVo> userReqVoList = list.stream().map(item -> {
            TaskUserReqVo taskUserReqVo = new TaskUserReqVo();
            BeanUtil.copyProperties(item, taskUserReqVo);
            UserVo userVo = userVoMapper.findUserById(item.getUserId());
            taskUserReqVo.setUserName(userVo.getName());
            return taskUserReqVo;
        }).collect(Collectors.toList());
        return userReqVoList;
    }
}
